<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>多维采集规则</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form label-width="100px" ref="editForm">
          <el-form-item label="">
            <p><span class="el-icon-warning infospan"></span>设置多维采集对应评分规则，管理者对应评分规则进行学生打分</p>
          </el-form-item>
          <el-form-item label="评分规则:">
            <v-selectpage :data="selectpagelist" key-field="id" show-field="title" class="form-control"
                          :tb-columns="columns" :multiple="true" v-model="domain_ids" @values="multipleValues"
                          placeholder="选择评分规则" title="选择评分规则" :page-size="10" ref="selectPoint">
            </v-selectpage>
            <div class="point-body">
              <div class="point-list">
                <div class="point-list-line">
                  <div class="point-list-line-list" v-for="(pointItem,pointIndex) in scoreList">
                    <span class="point-list-line-list-title">{{ pointItem.name }}</span>
                    <div class="point-list-line-list-input">
                      <el-input v-model="pointItem.scoreValue" auto-complete="off" placeholder="设定分值"
                                size="mini"></el-input>
                      分
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button type="primary" @click="submitForm" size="small">确定并提交</el-button>
        <el-button @click="closePOpup" size="small">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: ['visible','cornerId'], // commoent 学生点评 collection 学生采集
  components: {
  },
  data() {
    return {
      dialogVisible: false,
      activeName: 'first',
      detailName: '',
      editForm: {
        number: 0, // 可参与的人数
        pic: '', // 现场图
        name: '', // 区角名称
        section_id: '', // 年段
        class_id: '', // 班级
        user_id: '', //负责人id
        facility_id: '', // 识别器
        time: '', // 达标时长
        list: '', // 评分规则
        base_score: 0, // 基本分值
        join_class: '', // 可预约班级
        comment_user: '', // 管理人员
        description:'' // 设置目标
      }, // 提交数据
      detaiOption: '',
      adminUser: '',  // 负责人员
      assess: '', // 详情评分规则
      adminUserList: '', // 管理人员
      detailClass: '', // 预约班级
      detailDevice: [], // 设备信息
      multipleValuesList:[],
      scoreList:[
        {name: '优秀',scoreValue:0},
        {name:'良',scoreValue:0}
      ]
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if(this.visible){
        if(this.cornerId){
          this.getDetailCorner() // 获取详情数据
        }
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化
    async getDetailCorner() {
      console.log('get想起刚内容苹果呢')
      let data = await this.$fetch(this.api.admin_corner_edit, {
        id: this.cornerId
      });
      console.log('data:', data)
      let newlist = {}
      for(let i in this.editForm){
        newlist[i] = data.vo[i]
      }
      this.detailName = data.vo.name
      this.detaiOption = this.getJson(newlist)
      this.adminUser = this.getJson(data.vo.user)
      this.assess = this.getJson(data.vo.assess)
      this.detailClass = this.getJson(data.vo.join_class)
      this.adminUserList = this.getJson(data.vo.comment_user)
      this.detailDevice = this.getJson(data.vo.facility)
    },
    submitForm(formName) {
      console.log('this.editForm:', this.editForm)
      console.log('this.multipleValuesList:', this.multipleValuesList)
      if (!this.editForm.pic) {
        this.$message.error('请上传现场图')
        return
      }

      if (!this.editForm.name) {
        this.$message.error('区角名称不能为空')
        return
      }
      if (!this.editForm.facility_id) {
        this.$message.error('选择识别器id')
        return
      }
      if (!this.editForm.section_id || !this.editForm.class_id) {
        this.$message.error('完善区角位置信息')
        return
      }
      if (!this.editForm.time) {
        this.$message.error('填写达标时长')
        return
      }
      if (!this.editForm.number) {
        this.$message.error('填写参与人数')
        return
      }
      if (Number(this.editForm.base_score) <= 0) {
        this.$message.error('基本分值需大于0')
        return
      }
      // 属性评分判断
      let listScore = []
      for (let i in this.multipleValuesList) {
        let newList = {}
        let newDomain = []
        newList.fraction = []
        newList.items_id = this.multipleValuesList[i].id
        for (let n in this.multipleValuesList[i].domain_ids) {
          newDomain.push(this.multipleValuesList[i].domain_ids[n].id)
        }
        newList.domain_ids = newDomain.join(',')

        if (!this.multipleValuesList[i].scoreValue) {
          this.$message.error('请选择相应评分')
          return
        } else {
          for (let n in this.multipleValuesList[i].scoreList) {
            let newScoreList = {}
            if (!this.multipleValuesList[i].scoreList[n].scoreValue) {
              this.$message.error('请填写评分相应的分值')
              return
            }
            newScoreList.id = this.multipleValuesList[i].scoreList[n].id
            newScoreList.name = this.multipleValuesList[i].scoreList[n].name
            newScoreList.score = this.multipleValuesList[i].scoreList[n].scoreValue
            newList.fraction.push(newScoreList)
          }
        }
        listScore.push(newList)
      }

      console.log('领域内容：', this.multipleValuesList)
      console.log('领域内容listScore：', listScore)

      this.editForm.list = JSON.stringify(listScore)
      if (!this.cornerId && !listScore.length) {
        this.$message.error('编辑评分规则')
        return
      }
      if (this.cornerId && !listScore.length && this.is_assess) {
        this.$message.error('编辑评分规则')
        return
      }

      if (!this.editForm.join_class) {
        this.$message.error('选择可预约班级')
        return
      }
      this.submitFormAdd()
    },
    async submitFormAdd() {
      let option = this.getJson(this.editForm)
      if (this.cornerId) {
        option.id = this.cornerId
      }
      let data = await this.$post(this.cornerId?this.api.admin_corner_edit:this.api.admin_corner_add,option)
      console.log(data)
      if(!data.code){
        this.$message.success(data.msg)
        this.$emit('InitFn')
        this.closePOpup()
      }else{
        this.$message.error(data.msg)
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    // 参数变更进行更正
    getModleVal(option) {
      console.log('收到了新的数据内容：', option)
      if (option.type === 'multipleValuesList') {
        this.multipleValuesList = option.data
      }
      if (option.type === 'form') {
        for (let i in option.data) {
          this.editForm[i] = option.data[i]
        }
      }
    },
    getJson(option){
      return JSON.parse(JSON.stringify(option))
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 60%;
  height: 70%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.toolbarLine {
  text-align: center;
}
</style>


